package com.webapp.controller;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import com.webapp.model.LoginModel;

/**
 * Servlet implementation class Register
 */
public class Register extends HttpServlet {
	private static final long serialVersionUID = 1L;
       
    /**
     * @see HttpServlet#HttpServlet()
     */
    public Register() {
        super();
        // TODO Auto-generated constructor stub
    }


	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		
		// get the http session and form values from html form
		
		HttpSession session = request.getSession();
		
		String regType = (String)request.getParameter("regType");
		String firstName = (String)request.getParameter("firstTxt");
		String lastName = (String)request.getParameter("lastTxt");
		String address = (String)request.getParameter("addTxt");
		String city = (String)request.getParameter("cityTxt");
		String state = (String)request.getParameter("stateTxt");
		String zipS = (String)request.getParameter("zipTxt");
		String phoneS = (String)request.getParameter("phoneTxt");
		String email = (String)request.getParameter("emailTxt");
		String ssnS = (String)request.getParameter("ssnTxt");
		String password  = (String)request.getParameter("passTxt");
	
		int zip = Integer.parseInt(zipS);
		int ssn = Integer.parseInt(ssnS);
		int phone = Integer.parseInt(phoneS);
		
		// put strings in a data structure of list
		List regList = new ArrayList();
		regList.add(regType);
		regList.add(lastName);
		regList.add(address);
		regList.add(city);
		regList.add(zip);
		regList.add(phone);
		regList.add(email);
		regList.add(ssn);
		regList.add(password);
		
		
		// send it off to the model
		LoginModel lm = new LoginModel();
		if(lm.registerUser(regList) == true) {
			//redirect to login page
			response.sendRedirect("Login.jsp?login=reg");
		} else {
			//redirect to error
			response.sendRedirect("Error.jsp");
		}
		
		
	}

}
